package Interview.JiaShiJijin;

public class MemoryVisibilityDemo {
    private static boolean flag;
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10000; ++i) {
            flag = false;
            final int no = i;

            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    flag = true;
                    System.out.println(String.format("No.%d loop, t1 is done.", no));
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!flag) ;
                    System.out.println(String.format("No.%d loop, t2 is done.", no));
                }
            });

            t2.start();
            t1.start();

            t1.join();
            t2.join();
        }
    }
}
